<?php

$o = f()->db()->find_by_pkey('classroom','Classroom',$param);

// data form
if ($this->is_teacher) {
	if ($o === null) {
		echo 'Unknown classroom.';
	} else {
		$form = f()->form('add_classroom','New classroom')
			->text('name',t('Name'))
			->text('password',t('Password'))
			->text('description',t('Description'),array(
				'rows' => 5,
			))
			->button('submit',t('Save'));
		$form->load($o);
		if ($form->submitted()) {
			$fields = array('name','password','description');
			$form->save($o,$fields);
			$o->save();
			$this->redirect('@classroom/'.$o->_f_pkey_);
		} else {
			$form->render();
		}
	}
}

// grid
$results = f()->db()->select('student')
	->where('classroom_id = :classroom_id')
	->order('name')
	->exec(array(
		':classroom_id' => $o->_f_pkey_
	));
echo '<h2>Students</h2>';
if ($this->is_teacher) echo '<a href="'.$this->url('@student-add/'.$o->_f_pkey_).'">'.t('Add student').'</a>';
echo '<table class="gam-table">';
foreach ($results as $row) {
	echo '<tr>';
	echo "<td>{$row->name}</td>";
	echo '<td><img style="max-width: 100px; " src="'.$this->url('/files/'.$row->image,false).'"/></td>';
	if ($this->is_teacher) echo '<td><a href="'.$this->url('@student-edit/'.$row->id).'">'.t('Modify').'</a></td>';
	$results2 = f()->db()->select('appraisal')
		->where('student_id = :student_id')
		->order('date')
		->exec(array(
			':student_id' => $row->id
		));
	$value = 0;			
	foreach ($results2 as $row2) {
		$value += $row2->value;
		echo '<td><div>';
		echo "<h3>{$row2->name}</h3>";
		echo "<p>{$row2->value}</p>";
		echo "<p>{$row2->date}</p>";
		echo "<p>{$row2->comment}</p>";
		echo '<img style="max-width: 100px; " src="'.$this->url('/files/'.$row2->image,false).'"/></div>';
		if ($this->is_teacher) echo '<div><a href="'.
			$this->url('@appraisal-edit/'.$row2->id).'">'.t('Modify').'</a></div>';
		echo '</td>';
	}
	echo "<td><p>{$value}</p></td>";
	if ($this->is_teacher) echo '<td><a href="'.$this->url('@appraisal-add/'.$row->id).'">'.t('New').'</a></td>';
//	error_log(print_r(($row),1));
}
echo '</table>';
